from django.shortcuts import render,HttpResponse,reverse
from .models import User
# Create your views here.
def index_handler(request):
    context = request.context
    session_user = request.session['session_user']
    user = User.objects.get(id=session_user.get('id'))
    context['user'] = user
    return render(request,'index.html',context)




def login_handler(request):
    context = request.context
    if request.method != 'POST':
        return render(request, 'login.html')
    else:
         try:
             account = request.POST.get('account')
             password = request.POST.get('password')
             user_s = User.objects.filter(account=account,password=password)
             if user_s:
                 user = user_s[0]
                 request.session['session_user'] = {'id':user.id,'account':user.account}
                 return render(request,'index.html')
             else:
                 context['login_message'] = '账号或密码错误'
                 return render(request,'login.html',context)
         except:
            return HttpResponse(status=403)

